<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 13. < 对象(映射) & 字符串练习 > 提取身份信息
        const mapping = {
            北京市: 110000,
            天津市: 120000,
            河北省: 130000,
            山西省: 140000,
            内蒙古自治区: 150000,
        };

        const input = '张三:11234519980101001X';
        const output = {
            name: '张三',
            province: '北京市',
            birthday: '1998.1.1',
        };

        // ⭐
        function getUserInfo(input, mapping) {
            // 把mapping里面的每一项翻转过来 北京市：110000
            const newMapping = {};
            for (const key in mapping) {
                const value = mapping[key];
                newMapping[value] = key;
            }
            // 数组解构赋值
            const [name, idCard] = input.split(':');
            // 取出11，生成110000
            const provinceId = idCard.substr(0, 2) + '0000';
            const resValue = newMapping[provinceId];
            // 取出1998
            const year = idCard.substr(6, 4);
            // 取出01日变成.1
            const month = Number(idCard.substr(10, 2));
            // 取出01日变成.1
            const day = Number(idCard.substr(12, 2));
            const res = {
                name,
                province: resValue,
                birthday: `${year}.${month}.${day}`,
            };
            return res;
        }
        console.log(getUserInfo(input, mapping));
    </script>
</body>
</html>